using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class SalesController : Controller
    {
        //
        // GET: /TotalUpSales/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();


        public ActionResult Index()
        {
            int liststartY = 1988;
            int listendY = DateTime.Now.Year;
            List<string> listYear = new List<string>();
            for (int i = liststartY; i <= listendY; i++)
            {
                listYear.Add(i.ToString());
            }

            List<string> listMonth = new List<string>();
            for (int i = 1; i <= 12; i++)
            {
                listMonth.Add(i.ToString());
            }

            ViewData["StartYear"] = new SelectList(listYear);
            ViewData["EndYear"] = new SelectList(listYear);
            ViewData["Year"] = new SelectList(listYear);
            ViewData["Year2"] = new SelectList(listYear);
            ViewData["Month"] = new SelectList(listMonth);
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(FormCollection form)
        {
            try
            {
                int startY = int.Parse(form.Get("StartYear"));
                int endY = int.Parse(form.Get("EndYear"));
                int year = int.Parse(form.Get("Year"));
                int year2 = int.Parse(form.Get("Year2"));
                int month = int.Parse(form.Get("Month"));
                var isTotalYear = Request.Params.AllKeys.FirstOrDefault(key => key == "TotalYear");
                var isTotalMonth = Request.Params.AllKeys.FirstOrDefault(key => key == "TotalMonth");

                if (!string.IsNullOrEmpty(isTotalYear))
                {
                    return RedirectToAction("TotalUpByYear", new { startY, endY });
                }
                else if (!string.IsNullOrEmpty(isTotalMonth))
                {
                    return RedirectToAction("TotalUpByMonth", new { year });
                }
                else
                {
                    return RedirectToAction("TotalUpByDay", new { year2, month });
                }
            }
            catch (Exception ex)
            {
                return View(ex.Message);
            }
        }

        public ActionResult TotalUpByYear(int startY, int endY)
        {
            ViewData["startY"] = startY;
            ViewData["endY"] = endY;
            ViewData.Model = _db.HOA_DON;
            return View();
        }

        public ActionResult TotalUpByMonth(int year)
        {
            ViewData["Y"] = year;
            ViewData.Model = _db.HOA_DON;
            return View();
        }

        public ActionResult TotalUpByDay(int year2, int month)
        {
            ViewData["Y2"] = year2;
            ViewData["M"] = month;
            ViewData.Model = _db.HOA_DON;
            return View();
        }
    }
}
